<template>
  <div class="uuid-container">
    <div class="control-panel">
      <n-input-number v-model:value="count" :min="1" :max="100" placeholder="生成数量" class="count-input"/>
      <n-button type="primary" @click="generateUUIDs">
        生成 UUID
      </n-button>
      <n-button @click="copyAll" :disabled="uuids.length === 0">
        复制全部
      </n-button>
      <n-button @click="clearAll" :disabled="uuids.length === 0">
        清空
      </n-button>
    </div>
    <div class="result-panel">
      <div v-if="uuids.length === 0" class="empty-tip">
        点击生成按钮开始生成 UUID
      </div>
      <n-scrollbar v-else>
        <div v-for="(uuid, index) in displayUUIDs" :key="index" class="uuid-item" @click="copyUUID(uuid)">
          <span class="uuid-text">{{ uuid }}</span>
          <n-tooltip trigger="hover">
            <template #trigger>
              <n-button quaternary circle size="small">
                复制
              </n-button>
            </template>
            点击复制
          </n-tooltip>
        </div>
      </n-scrollbar>
      <div v-if="uuids.length > pageSize" class="pagination-container">
        <n-pagination v-model:page="currentPage" :page-size="pageSize" :item-count="uuids.length"/>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue'
import { v4 as uuidv4 } from 'uuid'
import view from '@renderer/util/ui/ViewUtil'

// 生成数量
const count = ref(5)
// UUID列表
const uuids = ref<string[]>([])
// 分页相关
const pageSize = 5
const currentPage = ref(1)
// 计算当前页显示的UUID
const displayUUIDs = computed(() => {
  const start = (currentPage.value - 1) * pageSize
  const end = start + pageSize
  return uuids.value.slice(start, end)
})
// 生成UUID
const generateUUIDs = () => {
  currentPage.value = 1
  uuids.value = Array(count.value)
    .fill(null)
    .map(() => uuidv4())
}
// 复制单个UUID
const copyUUID = async (uuid: string) => {
  try {
    await navigator.clipboard.writeText(uuid)
    view.alertSuccessMessage('复制成功')
  } catch (error) {
    view.alertErrorMessage('复制失败')
  }
}

// 复制全部
const copyAll = async () => {
  try {
    await navigator.clipboard.writeText(uuids.value.join('\n'))
    view.alertSuccessMessage('全部复制成功')
  } catch (error) {
    view.alertErrorMessage('复制失败')
  }
}

// 清空
const clearAll = () => {
  currentPage.value = 1
  uuids.value = []
}
</script>

<style scoped>
.uuid-container {
  height: 100%;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.control-panel {
  display: flex;
  gap: 8px;
  align-items: center;
}

.count-input {
  width: 120px;
}

.result-panel {
  flex: 1;
  border: 1px solid var(--border-color);
  border-radius: 4px;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.empty-tip {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-color-3);
}

.uuid-item {
  padding: 8px 16px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid var(--border-color);
  transition: background-color 0.2s;
}

.uuid-item:hover {
  background-color: var(--hover-color);
}

.uuid-text {
  font-family: monospace;
  user-select: all;
}

.pagination-container {
  padding: 8px;
  display: flex;
  justify-content: center;
  border-top: 1px solid var(--border-color);
}
</style>
